﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace DL.AI.AIGOAP
{
    public class BeliefFactory
    {
        private readonly GoapAgent m_Agent;
        private readonly Dictionary<string, AgentBelief> m_Beliefs;

        public BeliefFactory(GoapAgent agent, Dictionary<string, AgentBelief> beliefs)
        {
            m_Agent = agent;
            m_Beliefs = beliefs;
        }

        public void AddBelief(string key, Func<bool> condition)
        {
            m_Beliefs.Add(key, new AgentBelief.Builder(key).WithCondition(condition).Build());
        }

        public void AddLocationBelief(string key, float distance, Vector3 localtionCondition)
        {
            m_Beliefs.Add(key,
                new AgentBelief.Builder(key)
                    .WithCondition(() => InRangeOf(localtionCondition, distance))
                    .WithLocation(() => localtionCondition)
                    .Build());
        }

        public void AddSensorBelief(string key, Sensor sensor)
        {
            m_Beliefs.Add(key,
                new AgentBelief.Builder(key)
                    .WithCondition(() => sensor.IsTargetInRange)
                    .WithLocation(() => sensor.TargetPosition)
                    .Build());
        }

        private bool InRangeOf(Vector3 pos, float range) => Vector3.Distance(m_Agent.transform.position, pos) < range;
    }
}